package com.example.user

import com.blankj.utilcode.util.SPUtils
import com.example.base.mvi.BaseViewModel
import com.example.base.mvi.HttpType
import com.example.base.net.ApiService
import com.example.base.net.ShopApiService
import com.example.base.room.UserDao
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
@HiltViewModel
class UserViewModel @Inject constructor(val apiService: ApiService,val shopApiService: ShopApiService,val userDao: UserDao):BaseViewModel<UserIntent>() {
    override fun handleIntent(it: UserIntent) {
        when(it){
            is UserIntent.login -> {
            httpRequest(apiService.login(it.password,it.username))
            }

            is UserIntent.updatePwd -> {
                httpRequest(apiService.updatePwd(it.newPwd,it.userid))
            }

            UserIntent.category -> {
                httpRequest(shopApiService.category())
            }
            is UserIntent.product -> {
                //不能再次加载 除非添加刷新框架
                //httpRequest(shopApiService.product(it.limit,it.page),HttpType.PRODUCT)
                //Paging3分页请求
                //pageRequest({ProductDataSource(shopApiService)},HttpType.PRODUCT)
            }

            is UserIntent.roomAdd -> {
                roomRequest(flow { emit(userDao.insert(it.loginEntity)) },HttpType.ADD)
            }

            is UserIntent.roomDetail -> {
                roomRequest(flow { emit(userDao.getUserById(it.uid)) },HttpType.DETAIL)
            }

            is UserIntent.uploadHead -> {
                httpRequest(apiService.uploadHead(toPart(it.path,it.fileName),SPUtils.getInstance().getInt("uid").toString()))
            }
        }
    }
}